﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace M3G {
    public class PointSpriteMode : Object3D {
        #region Field
        float constantAttenuation;
        float linearAttenuation;
        float quadraticAttenuation;
        float minPointSize;
        float maxPointSize;
        float fadeThresholdSize;
        #endregion

        #region Method
        public PointSpriteMode () {
            this.constantAttenuation = 1.0f;
            this.linearAttenuation = 0.0f;
            this.quadraticAttenuation = 0.0f;
            this.minPointSize = 0.0f;
            this.maxPointSize = 1.0f;
            this.fadeThresholdSize = 1.0f;
        }

        protected PointSpriteMode (PointSpriteMode pointSpriteMode) : base(pointSpriteMode) {
            this.constantAttenuation  = pointSpriteMode.constantAttenuation;
            this.linearAttenuation    = pointSpriteMode.linearAttenuation;
            this.quadraticAttenuation = pointSpriteMode.quadraticAttenuation;
            this.minPointSize         = pointSpriteMode.minPointSize;
            this.maxPointSize         = pointSpriteMode.maxPointSize;
            this.fadeThresholdSize    = pointSpriteMode.fadeThresholdSize;
        }

        public float[] PointParameters {
            get {
                return new float[] { constantAttenuation, linearAttenuation, quadraticAttenuation, minPointSize, maxPointSize, fadeThresholdSize };
            }
        }

        public float PointFadeThreshold {
            set {
                var threshold = value;
                if (threshold < 0) {
                    throw new ArgumentException ("Fade threshold must be within [0,inf], th=" + threshold);
                }

                this.fadeThresholdSize = threshold;
            }
        }

        public void SetPointSizeAttenuation (float constant, float linear, float quadratic) {
            if (constant < 0 || linear < 0 || quadratic < 0) {
                throw new ArgumentException ("Attenuation parameters must be [0,inf], c=" + constant + ",l=" + linear + ",q=" + quadratic);
            }
            if (constant == 0 && linear == 0 && quadratic == 0) {
                throw new ArgumentException ("One of attenuation parameters must be positive number");
            }

            this.constantAttenuation = constant;
            this.linearAttenuation = linear;
            this.quadraticAttenuation = quadratic;
        }

        public void SetPointSizeClamp (float minSize, float maxSize) {
            if (minSize < 0 || maxSize < 0) {
                throw new ArgumentException ("MinSize and Max size must be within [0,inf], min=" + minSize + ",max=" + maxSize);
            }
            if (maxSize < minSize) {
                throw new ArgumentException ("MaxSize must be larger than MinSize, min=" + minSize + ",max=" + maxSize);
            }

            this.minPointSize = minSize;
            this.maxPointSize = maxSize;
        }

        public override Object3D Duplicate () {
            return new PointSpriteMode (this);
        }

        #endregion
    }
}
